<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.types
 */

/**
 * A UUID Identifier.
 */
class MUUID {

    /**
     * UUID value
     *
     * @var string
     */
    private $uuid;

    /**
     * Create a UUID
     *
     * @param string $value UUID value
     */
    public function MUUID($uuid = '00000000-0000-0000-0000-000000000000') {
        // Check value
        if(!$this->validate($uuid))
        {
            throw new Exception('Malformed UUID');
        }

        // Set hypenated value 
        $this->uuid = $this->hyphenated($uuid);
    }

    /**
     * Return the uuid hypenathed
     *
     * @return string hypenathed uuid
     */
    public function getHyphenated() {
        return $this->uuid;
    }

    /**
     * Return the uuid without hyphens
     *
     * @return string unhyphenated uuid
     */
    public function getUnHyphenated() {
        return str_replace('-', '', $this->uuid);
    }

    /**
     * Check if the uuid is valid
     *
     * @param string $uuid The uuid
     * @return boolean True if the uuid is valid, false otherwise
     */
    public static function validate($uuid) {
        // The uuid is hypenathed
        if(strlen($uuid) == 36)
        {
            // Check hyphen positions 
            if($uuid[8] != '-' || $uuid[13] != '-'
                || $uuid[18] != '-' || $uuid[23] != '-')
                    return false;
        }

        // Remove any strange digit - and others 
        $uuid = preg_replace('/[^a-f0-9]/i', '', $uuid);

        // The uuid length musy be 32
        return strlen($uuid) == 32;
    }

    /**
     * Hypenathe the uuid
     *
     * @param string $uuid a valid UUID
     * @return string hyphenated uuid 
     */
    private function hyphenated($uuid) {
        // Remove hyphens and convert to lowercase
        $uuid = str_replace('-','',strtolower($uuid));

        // Add hyphens
        return substr($uuid,0,8) . '-' . substr($uuid,8,4) . '-' .
            substr($uuid,12,4) . '-' . substr($uuid,16,4) . '-' .
            substr($uuid,20,12);
    }

    /**
     * Return a new random uuid
     *
     * @return MUUID The new random uuid 
     */
    public static function random() {
        // Generate a random value string 
        $value = md5(mt_rand(0,2000000000));
        // Return the new uuid 
        return new MUUID($value);
    }

    /**
     * Return the string representation of the object
     *
     * @return string string representation of the object
     */
    public function __toString() {
        return 'UUID: ' . $this->uuid;
    }
}
?>